using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 向量的点乘 + 叉乘  (进阶)
/// </summary>
public class Lecture02_2 : MonoBehaviour
{
    // 两向量叉乘，得到两向量之间的夹角，可以得到这两个物体之间的位置关系

    public Transform role;

    public Transform enemy;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    /// <summary>
    /// 检测位置(带方位)
    /// </summary>
    public void CheckEnemyDir()
    {
        if (this.GetDot() > 0)
        {
            if (GetTargetCross() > 0)
            {
                Debug.LogError("敌人在左前方");
            }
            else
            {
                Debug.LogError("敌人在右前方");
            }
            
        }
        else
        {
            if (GetTargetCross() > 0)
            {
                Debug.LogError("敌人在左后方");
            }
            else
            {
                Debug.LogError("敌人在右后方");
            }
        }
    }

    private float GetDot()
    {
        float dotVal = Vector3.Dot(GetRoleV3(),GetEnemyV3());
        return dotVal;
    }

    /// <summary>
    /// 玩家
    /// </summary>
    /// <returns></returns>
    private Vector3 GetRoleV3()
    {
        if (role)
        {
            Vector3 roleV3 = role.transform.forward - role.transform.position;
            return roleV3;
        }
        return Vector3.one;
    }

    /// <summary>
    /// 敌人
    /// </summary>
    /// <returns></returns>
     private Vector3 GetEnemyV3()
    {
        if (role && enemy)
        {
            Vector3 roleV3 = enemy.transform.position - role.transform.position;
            return roleV3;
        }
        return Vector3.one;
    }

    /// <summary>
    /// z 轴正方向(朝向)的向量
    /// </summary>
    /// <returns></returns>
    private Vector3 GetRoleFV3()
    {
        return new Vector3(role.transform.position.x,role.transform.position.y,role.transform.forward.z);
    }

    private Vector3 GetRoleF()
    {
        return GetRoleFV3() - role.transform.position;
    }

    /// <summary>
    /// 目标位置叉乘的方向
    /// </summary>
    /// <returns></returns>
    private float GetTargetCross()
    {
        Vector3 tV3 = new Vector3(enemy.transform.position.x,role.transform.position.y,enemy.transform.position.z);
        Vector3 tTarget = tV3 - GetRoleFV3();

        return Vector3.Cross(tTarget,GetRoleF()).normalized.y;
    }
}
